//-----------------------------------------------------------------------
// <copyright file="ProductBuyDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ProductBuyDAOBase
    {
        #region Common methods
        public virtual ProductBuy CreateProductBuyFromReader(IDataReader reader)
        {
            ProductBuy item = new ProductBuy();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ProductId"))) item.ProductId = (long)reader["ProductId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Url"))) item.Url = (string)reader["Url"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("BriefDescription"))) item.BriefDescription = (string)reader["BriefDescription"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberId"))) item.MemberId = (long)reader["MemberId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThumbnailImage"))) item.ThumbnailImage = (string)reader["ThumbnailImage"];
                if (!reader.IsDBNull(reader.GetOrdinal("EndDate"))) item.EndDate = (DateTime)reader["EndDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatId"))) item.CatId = (long)reader["CatId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Viewed"))) item.Viewed = (long)reader["Viewed"];
                if (!reader.IsDBNull(reader.GetOrdinal("ObjectId"))) item.ObjectId = (long)reader["ObjectId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("ActivateCode"))) item.ActivateCode = (string)reader["ActivateCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("ContactOption"))) item.ContactOption = (int)reader["ContactOption"];
                if (!reader.IsDBNull(reader.GetOrdinal("ExpireDate"))) item.ExpireDate = (DateTime)reader["ExpireDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("DetailDescription"))) item.DetailDescription = (string)reader["DetailDescription"];
                if (!reader.IsDBNull(reader.GetOrdinal("PriceRangeUnitId"))) item.PriceRangeUnitId = (long)reader["PriceRangeUnitId"];
                if (!reader.IsDBNull(reader.GetOrdinal("PriceRangeSmall"))) item.PriceRangeSmall = (string)reader["PriceRangeSmall"];
                if (!reader.IsDBNull(reader.GetOrdinal("PriceRangeLarge"))) item.PriceRangeLarge = (string)reader["PriceRangeLarge"];
                if (!reader.IsDBNull(reader.GetOrdinal("QuantityRequire"))) item.QuantityRequire = (string)reader["QuantityRequire"];
                if (!reader.IsDBNull(reader.GetOrdinal("QuantityRequireTypeId"))) item.QuantityRequireTypeId = (long)reader["QuantityRequireTypeId"];
                if (!reader.IsDBNull(reader.GetOrdinal("RequiredCertification"))) item.RequiredCertification = (string)reader["RequiredCertification"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProductBuyFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateProductBuy methods
            
        public virtual void CreateProductBuy(ProductBuy productBuy)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductBuyInsert");
                
                database.AddInParameter(dbCommand, "@Url", DbType.String, productBuy.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, productBuy.Title);
                database.AddInParameter(dbCommand, "@BriefDescription", DbType.String, productBuy.BriefDescription);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, productBuy.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, productBuy.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, productBuy.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, productBuy.Order);
                database.AddInParameter(dbCommand, "@Image", DbType.String, productBuy.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, productBuy.ThumbnailImage);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, productBuy.EndDate);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, productBuy.CatId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, productBuy.Viewed);
                database.AddInParameter(dbCommand, "@ObjectId", DbType.Int64, productBuy.ObjectId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, productBuy.StatusId);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, productBuy.Enable);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, productBuy.ActivateCode);
                database.AddInParameter(dbCommand, "@ContactOption", DbType.Int32, productBuy.ContactOption);
                database.AddInParameter(dbCommand, "@ExpireDate", DbType.DateTime, productBuy.ExpireDate);
                database.AddInParameter(dbCommand, "@DetailDescription", DbType.String, productBuy.DetailDescription);
                database.AddInParameter(dbCommand, "@PriceRangeUnitId", DbType.Int64, productBuy.PriceRangeUnitId);
                database.AddInParameter(dbCommand, "@PriceRangeSmall", DbType.String, productBuy.PriceRangeSmall);
                database.AddInParameter(dbCommand, "@PriceRangeLarge", DbType.String, productBuy.PriceRangeLarge);
                database.AddInParameter(dbCommand, "@QuantityRequire", DbType.String, productBuy.QuantityRequire);
                database.AddInParameter(dbCommand, "@QuantityRequireTypeId", DbType.Int64, productBuy.QuantityRequireTypeId);
                database.AddInParameter(dbCommand, "@RequiredCertification", DbType.String, productBuy.RequiredCertification);
                database.AddOutParameter(dbCommand, "@ProductId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                productBuy.ProductId = (long)database.GetParameterValue(dbCommand, "@ProductId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateProductBuyException, ex);
            }
        }

        #endregion

        #region UpdateProductBuy methods
        
        public virtual void UpdateProductBuy(ProductBuy productBuy)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductBuyUpdate");            
                
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, productBuy.ProductId);
                database.AddInParameter(dbCommand, "@Url", DbType.String, productBuy.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, productBuy.Title);
                database.AddInParameter(dbCommand, "@BriefDescription", DbType.String, productBuy.BriefDescription);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, productBuy.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, productBuy.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, productBuy.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, productBuy.Order);
                database.AddInParameter(dbCommand, "@Image", DbType.String, productBuy.Image);
                database.AddInParameter(dbCommand, "@ThumbnailImage", DbType.String, productBuy.ThumbnailImage);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, productBuy.EndDate);
                database.AddInParameter(dbCommand, "@CatId", DbType.Int64, productBuy.CatId);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, productBuy.Viewed);
                database.AddInParameter(dbCommand, "@ObjectId", DbType.Int64, productBuy.ObjectId);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, productBuy.StatusId);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, productBuy.Enable);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, productBuy.ActivateCode);
                database.AddInParameter(dbCommand, "@ContactOption", DbType.Int32, productBuy.ContactOption);
                database.AddInParameter(dbCommand, "@ExpireDate", DbType.DateTime, productBuy.ExpireDate);
                database.AddInParameter(dbCommand, "@DetailDescription", DbType.String, productBuy.DetailDescription);
                database.AddInParameter(dbCommand, "@PriceRangeUnitId", DbType.Int64, productBuy.PriceRangeUnitId);
                database.AddInParameter(dbCommand, "@PriceRangeSmall", DbType.String, productBuy.PriceRangeSmall);
                database.AddInParameter(dbCommand, "@PriceRangeLarge", DbType.String, productBuy.PriceRangeLarge);
                database.AddInParameter(dbCommand, "@QuantityRequire", DbType.String, productBuy.QuantityRequire);
                database.AddInParameter(dbCommand, "@QuantityRequireTypeId", DbType.Int64, productBuy.QuantityRequireTypeId);
                database.AddInParameter(dbCommand, "@RequiredCertification", DbType.String, productBuy.RequiredCertification);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateProductBuyException, ex);
            }
        }
        
        #endregion

        #region DeleteProductBuy methods
        public virtual void DeleteProductBuy(long productId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductBuyDelete");
                
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, productId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteProductBuyException, ex);
            }
        }

        #endregion

        #region GetProductBuy methods
        
        public virtual ProductBuy GetProductBuy(long productId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductBuyGet");
                
                database.AddInParameter(dbCommand, "@ProductId", DbType.Int64, productId);
                
                ProductBuy productBuy =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        productBuy = CreateProductBuyFromReader(reader);
                        reader.Close();
                    }
                }
                return productBuy;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductBuyException, ex);
            }
        }
        
        #endregion

        #region GetProductBuyList methods
        public virtual ProductBuyCollection GetProductBuyList(ProductBuyColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductBuyGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProductBuyCollection productBuyCollection = new ProductBuyCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ProductBuy productBuy = CreateProductBuyFromReader(reader);
                        productBuyCollection.Add(productBuy);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return productBuyCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductBuyListException, ex);
            }
        }
        
        public virtual ProductBuyCollection GetProductBuyList(ProductBuyColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetProductBuyList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetProductBuyList methods
        public virtual ProductBuyCollection GetProductBuyListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spProductBuyGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ProductBuyCollection productBuyCollection = new ProductBuyCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ProductBuy productBuy = CreateProductBuyFromReader(reader);
                        productBuyCollection.Add(productBuy);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return productBuyCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetProductBuyListException, ex);
            }
        }
        
        public virtual ProductBuyCollection GetProductBuyListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetProductBuyListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}